/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package data;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Ashish
 *
 */
public class Image {
	private ArrayList<Double>histogram = new ArrayList<Double>();
	private Double classLabel;
	private String imageName;
	private ArrayList<FeatureVector> descriptors = new ArrayList<FeatureVector>();
	public ArrayList<ArrayList<Integer>> genePool = null;
	/**
	 * @param string
	 * @param integer
	 * @param resourceLocation
	 * @throws IOException 
	 */
	public Image(String imageName, Double classLabel, String resourceLocation) throws IOException {
		this.setImageName(imageName);
		this.setClassLabel(classLabel);
		this.setDescriptors(resourceLocation);
	}
	/**
	 * @param resourceLocation
	 * @throws IOException 
	 */
	private void setDescriptors(String resourceLocation) throws IOException {
		String descriptorFilename = setDescriptorFilename(resourceLocation);
		File file = new File(descriptorFilename);
		Reader fr = new FileReader(file);
		StreamTokenizer st = new StreamTokenizer(fr);
		st.parseNumbers();
		st.nextToken();
		int numFeatureVectors = (int) st.nval;
		st.nextToken();
		int numDescriptorDimensions = (int) st.nval;
		for(int i = 0; i < numFeatureVectors; i++){
			FeatureVector fv = new FeatureVector(numDescriptorDimensions);
			for(int j=0;j<4;j++){
				st.nextToken();
			}
			for(int j=0;j<numDescriptorDimensions;j++){
				st.nextToken();
				int feature = (int) st.nval;
				fv.setFeatureVector(feature);
			}
			this.descriptors.add(fv);
		}
	}
	/**
	 * @param resourceLocation
	 * @return
	 */
	private String setDescriptorFilename(String resourceLocation) {
		StringBuilder sb = new StringBuilder("");
		sb.append(resourceLocation);
		sb.append(this.imageName);
		sb.append(".key");// the filename extension for DavidLowe's SIFT descriptor file
		return sb.toString();
	}
	/**
	 * @param histogram the histogram to set
	 */
	public void setHistogram(ArrayList<Double> histogram) {
		this.histogram = histogram;
	}
	/**
	 * @return the histogram
	 */
	public Collection<? extends Double> getHistogram() {
		return histogram;
	}
	/**
	 * @param classLabel the classLabel to set
	 */
	public void setClassLabel(Double classLabel) {
		this.classLabel = classLabel;
	}
	/**
	 * @return the classLabel
	 */
	public Double getClassLabel() {
		return classLabel;
	}
	/**
	 * @param descriptors the descriptors to set
	 */
	public void setDescriptors(ArrayList<FeatureVector> descriptors) {
		this.descriptors = descriptors;
	}
	/**
	 * @return the descriptors
	 */
	public ArrayList<FeatureVector> getDescriptors() {
		return descriptors;
	}
	/**
	 * @param imageName the imageName to set
	 */
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}
	/**
	 * @return the imageName
	 */
	public String getImageName() {
		return imageName;
	}

}
